helper.ts ➔ outputDivision   B
last analyzed

Complexity

Conditions 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 20
rs 7.3333
c 0
b 0
f 0
cc 8
1
export function mapMatchStatus(statusCode) {
2
  const statusCodes = new Map([
3
    [`PP`, `Uitgesteld`],
4
    [`ST`, `Stopgezet`],
5
    [`F1`, `Forfait`],
6
    [`FI`, `Forfait`],
7
    [`F2`, `Forfait`],
8
    [`FF`, `Forfait`],
9
    [`AMC`, `Algemeen forfait`],
10
  ])
11
12
  return statusCodes.get(statusCode) || null
13
}
14
15
/**
16
 * Strip first character if the division matches a youth division structure AND starts with a number.
17
 *
18
 * General form:
19
 *  - G9E
20
 *  - 2G10G
21
 *  - P7L
22
 *  - 2P12M
23
 * The function should return true if it matches the form starting with a "2".
24
 *
25
 * @param {string} division
26
 */
27
export function isYouthDivisionWithNumericFirst(division) {
28
  return /^(\d+)([a-zA-Z]+)(\d*)([a-zA-Z])/.test(division)
29
}
30
31
/**
32
 * Remove the first character of a division string if it's a number.
33
 *
34
 * Numbers are sometimes added for youth divisions to indicate a second period within a same season.
35
 * For example, G9K is the regional U9 division K before new year's day. From January 1st, the teams
36
 * can be re-arranged in a new (more balanced) division, which will be named something like 2G2K,
37
 * with the "2" in front indicating this difference.
38
 *
39
 * @param {string} division
40
 */
41
export function replaceFirstCharIfNumber(division) {
42
  if (isYouthDivisionWithNumericFirst(division)) {
43
    // Remove first character.
44
    division = division.substr(1)
45
  }
46
47
  return division
48
}
49
50
/**
51
 * Convert a region+division into an output label.
52
 *
53
 * @param {array} divisionArray
54
 * @param {string} level
55
 */
56
export function outputDivision(divisionArray, level = ``) {
57
  if (divisionArray[0] === `BCA`) {
58
    return `Beker van Brabant`
59
  } else if (divisionArray[0] === `ESCA`) {
60
    return `Beker voor B-ploegen`
61
  } else if (divisionArray[0] === `FR`) {
62
    return `Vriendschappelijk`
63
  } else if (divisionArray[0] === `BVZ`) {
64
    return `Beker van Zemst`
65
  } else if (divisionArray[2] <= 4) {
66
    return `${divisionArray[2]}e ${level !== `nat` ? `Prov.` : `Nationale`} ${divisionArray[3]}`
67
  } else {
68
    return `U${divisionArray[2]} / ${divisionArray[3]}${divisionArray[4] ? ` / ${divisionArray[4]}` : ``}`
69
  }
70
}
71
72
/**
73
 * Replace a divisionCode with its descriptive label.
74
 *
75
 * @param {string} division
76
 */
77
export function mapDivision(division) {
78
  return /^([A-Z]+)?(\d+)?([a-zA-Z]+)(\d*)$/.exec(replaceFirstCharIfNumber(division))
79
}
80
81
/**
82
 * Retrieve mapping and the formatted descriptive label of a division.
83
 *
84
 * @param {string} division
85
 * @param {string} region
86
 */
87
export function formatDivision(division, region) {
88
  const divisionArr = mapDivision(division)
89
  return outputDivision(divisionArr, region)
90
}
91
92
/**
93
 * Truncate to <n> letters and optionally stop at the last word instead of letter.
94
 *
95
 * @param {int} size
96
 * @param {boolean} useWordBoundary
97
 */
98
export function truncate(size, useWordBoundary = true) {
99
  if (this.length <= size) {
100
    return this
101
  }
102
  const subString = this.substr(0, size - 1)
103
  return (useWordBoundary ? subString.substr(0, subString.lastIndexOf(` `)) : subString) + `…`
104
}
105
106
export function mapPsdStatusIcon(statusCode) {
107
  const statusCodes = new Map([
108
    [0, ``],
109
    [1, `fa-times`],
110
    [2, `fa-times`],
111
    [3, `fa-ban`],
112
  ])
113
114
  return statusCodes.get(statusCode) || null
115
}
116
117
export function translateGameResult(result) {
118
  const statusCodes = new Map([
119
    [`WON`, `Gewonnen`],
120
    [`EQUAL`, `Gelijkgespeeld`],
121
    [`LOST`, `Verloren`],
122
  ])
123
  return statusCodes.get(result) || null
124
}
125
126
export function capitalizeFirstLetter(string) {
127
  return string.charAt(0).toUpperCase() + string.slice(1)
128
}
129
130
export function groupByDate(data) {
131
  const groups = data.reduce((groups, object) => {
132
    const date = object.start.split(` `)[0]
133
    if (!groups[date]) {
134
      groups[date] = []
135
    }
136
    groups[date].push(object)
137
    return groups
138
  }, {})
139
140
  // Edit: to add it in the array format instead
141
  const groupArrays = Object.keys(groups).map((date) => {
142
    return {
143
      date,
144
      objects: groups[date],
145
    }
146
  })
147
148
  return groupArrays
149
}
150
151
export function groupByMonth(data) {
152
  const groups = data.reduce((groups, object) => {
153
    const date = new Date(object.timestamp * 1000)
154
    const month = date.toLocaleString(`nl-BE`, { month: `long` })
155
    if (!groups[month]) {
156
      groups[month] = []
157
    }
158
    groups[month].push(object)
159
    return groups
160
  }, {})
161
162
  const groupArrays = Object.keys(groups).map((date) => {
163
    return {
164
      date,
165
      objects: groups[date],
166
    }
167
  })
168
169
  return groupArrays
170
}
171
172
export function replaceAll(source: string, search: string, replacement: string) {
173
  return source.replace(new RegExp(search, `g`), replacement)
174
}
175
176
export default {
177
  mapMatchStatus,
178
  mapDivision,
179
  formatDivision,
180
  truncate,
181
  mapPositionCode,
182
  getPositions,
183
  mapPsdStatus,
184
  mapPsdStatusShort,
185
  mapPsdStatusIcon,
186
  translateGameResult,
187
  capitalizeFirstLetter,
188
  groupByDate,
189
  groupByMonth,
190
  replaceAll,
191
  sortRankings,
192
}
193